<script type="text/javascript" src="../../static/js/prompt.js" th:src="@{/js/prompt.js}"></script>
<div class="x_panel">
    <div class="x_title">
        <h2>角色
            <small>修改</small>
        </h2>
        <div class="clearfix"></div>
    </div>
    <div class="x_content">
        <br/>
        <form id="defaultForm" th:object="${role}" data-parsley-validate=""
              class="form-horizontal form-label-left" novalidate="">
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleName">角色名称
                </label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <input name="roleId" type="hidden" th:value="*{roleId}"/>
                    <input type="text" id="roleName" name="roleName" required="required" th:value="*{roleName}"
                           oninvalid="setCustomValidity('请输入角色名称');" class="form-control col-md-7 col-xs-12"/>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleCode">角色编码
                </label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <input type="text" id="roleCode" name="roleCode" required="required" th:value="*{roleCode}"
                           oninvalid="setCustomValidity('请输入角色编码');" class="form-control col-md-7 col-xs-12"/>
                </div>
            </div>
            <div class="form-group">
                <label for="roleContent" class="control-label col-md-3 col-sm-3 col-xs-12">角色描述</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <textarea id="roleContent" class="form-control col-md-7 col-xs-12" name="roleContent"
                              th:text="*{roleContent}"
                              required="required" oninvalid="setCustomValidity('请输入角色描述');"></textarea>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">

                    <button class="btn btn-primary" th:id="*{roleId}" onclick="updateTo(this)" id="resetBtn" type="button">重置</button>
                    <button type="button" id="validateBtn" class="btn btn-primary">保存</button>
                    <button id="back"  class="btn btn-success" type="button">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript" th:inline="javascript">

    /*<![CDATA[*/
    $(function () {
        $("#back").click(function () {
            $.goTo(/*[[@{toRoleList}]]*/"./role_list.html");
        });
    })

    $(document).ready(function () {
        $('form').bootstrapValidator({
//        live: 'disabled',
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
//            submitHandler: function (form) {
//
//                $('form').ajaxSubmit({
//                    success: function (d) {
//                    }
//                });
//
//            },
            fields: {  //startTimeToDate
                roleName: {
                    validators: {
                        notEmpty: {
                            message: '请输入角色名称'
                        }
                    }
                },
                roleCode: {
                    validators: {
                        notEmpty: {
                            message: '请输入角色编码'
                        }
                    }
                },
                roleContent: {
                    validators: {
                        notEmpty: {
                            message: '请输入角色描述'
                        }
                    }
                }

            }
        });
        var roleCode=$("#roleCode").val();
        var roleName=$("#roleName").val();
        // Validate the form manually
        $('#validateBtn').click(function() {
            $('form').bootstrapValidator('validate');
            var flag = $('form').data('bootstrapValidator').isValid();
            if(flag){
                var SaveAndFlush = $("#defaultForm").serialize();
                $('#validateBtn').attr("disabled","disabled");
                $.post("/role/checkRoleUpdate1", SaveAndFlush, function (result) {
                    if(result=="roleCode"){
                        $.MsgBox.Alert("提示", "角色编码重复，请重新填写");
                        $('#validateBtn').removeAttr("disabled","disabled")
                        $("#roleCode").val(roleCode);
                    }else if(result=="roleName"){
                        $.MsgBox.Alert("提示", "角色名称重复，请重新填写");
                        $('#validateBtn').removeAttr("disabled","disabled")
                        $("#roleName").val(roleName);
                    }else {
                        $.post("/role/updateAndSave", SaveAndFlush, function () {
                            $.MsgBox.Alert("提示", "修改成功");
                            $('#validateBtn').removeAttr("disabled","disabled")
                            $.goTo("toRoleList");
                        });
                    }
                });
            }else{
                return;
            }
        });

        $('#resetBtn').click(function() {
            $('form').data('bootstrapValidator').resetForm(true);
        });
    });
    /*]]>*/
</script>